package quick.start.ui.widget;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Html.ImageGetter;
import android.widget.TextView;

public class UrlImageParser implements ImageGetter, ImageGetterAsyncTask.OnDrawableLoadCallback {

	 private TextView mContainer;
	 
    public UrlImageParser(TextView t) {
        mContainer = t;
    }
    
	@Override
	public void onDrawableLoadComplete(Drawable d) {
        // redraw the image by invalidating the mContainer
        mContainer.invalidate();

        if (Build.VERSION.SDK_INT >= 14) {
            // For ICS
            mContainer.setMaxHeight((mContainer.getMeasuredHeight() + d.getIntrinsicHeight()));
        } else {
            // Pre ICS
            mContainer.setEllipsize(null);
        }
        // redraw the image by invalidating the mContainer
        mContainer.invalidate();
	}

	@Override
	public Drawable getDrawable(String source) {
        UrlDrawable urlDrawable = new UrlDrawable();

        // get the actual source
        ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask(urlDrawable);

        asyncTask.setOnDrawableLoadCallback(this);
        asyncTask.execute(source);

        // return reference to URLDrawable where I will change with actual image
        // from
        // the src tag
        return urlDrawable;
	}

}
